package com.zbcbms.controller;

import com.zbcbms.common.AlertUtilCommon;
import com.zbcbms.entity.dto.AddBookBorrowDto;
import com.zbcbms.entity.tableData.BookBorrowTableData;
import com.zbcbms.entity.dto.UserMap;
import com.zbcbms.service.BookService;
import com.zbcbms.service.UserService;
import com.zbcbms.service.impl.BookBorrowServiceImpl;
import com.zbcbms.service.impl.BookServiceImpl;
import com.zbcbms.service.impl.UserServiceImpl;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author fengq
 */
public class UpdateBorrowController implements Initializable {
    @FXML
    private ComboBox<String> updateBookNumber;
    @FXML
    private ComboBox<String> updateUserNumber;
    @FXML
    private ComboBox<String> updateBorrowStatus;
    @FXML
    private TextField updateOperator;
    @FXML
    private DatePicker updateDeadline;
    @FXML
    private DatePicker updateReturnDate;
    @FXML
    private DatePicker updateBorrowDate;
    @FXML
    private TextField updateBorrowId;
    @FXML
    private TextField updateBookName;
    @FXML
    private TextField updateUserName;
    @FXML
    private AnchorPane updateBorrowAnchorPane;

    private UserService userService=new UserServiceImpl();

    private BookService bookService=new BookServiceImpl();

    private AlertUtilCommon alertUtilCommon=new AlertUtilCommon();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        List<String> list=userService.getAllUserNumber();
        ObservableList<String> obList1 = FXCollections.observableArrayList(list);
        updateUserNumber.setItems(obList1);

        List<String> snackList=bookService.getAllBookNumberWithStatus();
        ObservableList<String> obList2 = FXCollections.observableArrayList(snackList);
        updateBookNumber.setItems(obList2);

        List<String> list2= Arrays.asList("借用中","已归还");
        ObservableList<String> observableList = FXCollections.observableArrayList(list2);
        updateBorrowStatus.setItems(observableList);
    }

    public void updateTextInit(BookBorrowTableData bookBorrowTableData){
        updateBorrowId.setText(bookBorrowTableData.getId().get());
        updateBookName.setText(bookBorrowTableData.getBookName().get());
        updateBookNumber.getEditor().setText(bookBorrowTableData.getBookNumber().get());
        updateOperator.setText(bookBorrowTableData.getOperatorName().get());
        updateBorrowDate.getEditor().setText(bookBorrowTableData.getBorrowDate().get());
        updateDeadline.getEditor().setText(bookBorrowTableData.getDeadline().get());
        updateUserNumber.getEditor().setText(bookBorrowTableData.getUserNumber().get());
        updateUserName.setText(bookBorrowTableData.getUserName().get());
        updateBorrowStatus.setValue(bookBorrowTableData.getStatus().get());
        updateReturnDate.getEditor().setText(bookBorrowTableData.getReturnDate().get());
        if (updateBorrowStatus.getValue().equals("借用中")){
            updateReturnDate.setDisable(true);
        }
    }

    @FXML
    public void userNumberReleased(){
        List<String> snackList=userService.getUserNumberByKeyword(updateUserNumber.getEditor().getText());
        ObservableList<String> obList = FXCollections.observableArrayList(snackList);
        updateUserNumber.setItems(obList);
        updateUserNumber.show();
    }
    @FXML
    public void updateBorrowAction(){
        String bookBorrowId=updateBorrowId.getText();
        String bookNumber=updateBookNumber.getEditor().getText();
        String userNumber=updateUserNumber.getEditor().getText();
        String operator= UserMap.getName();
        String status=updateBorrowStatus.getValue();
        Date borrowTime=null;
        Date deadline=null;
        Date returnTime=null;
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            borrowTime=simpleDateFormat.parse(updateBorrowDate.getEditor().getText());
            deadline=simpleDateFormat.parse(updateDeadline.getEditor().getText());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Boolean aBoolean=null;
        if (status.equals("已归还")){
            try {
                returnTime=simpleDateFormat.parse(updateReturnDate.getEditor().getText());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (borrowTime.compareTo(returnTime)>0|| borrowTime.compareTo(deadline)>0){
                alertUtilCommon.AlterMessageInform(updateBookName.getScene().getWindow(), Alert.AlertType.ERROR,"错误提醒",
                        "时间参数不合法","小仲书：可惜，差亿点点成功");
            }else {
                AddBookBorrowDto addBookBorrowDto=new AddBookBorrowDto(bookBorrowId,bookNumber,borrowTime,returnTime,deadline,status,operator,userNumber);
                aBoolean= new BookBorrowServiceImpl().updateById(addBookBorrowDto);
            }
        }else {
            if (borrowTime.compareTo(deadline)>0){
                alertUtilCommon.AlterMessageInform(updateBookName.getScene().getWindow(), Alert.AlertType.ERROR,"错误提醒",
                        "时间参数不合法","小仲书：可惜，差亿点点成功");
            }else {
                AddBookBorrowDto addBookBorrowDto=new AddBookBorrowDto(bookBorrowId,bookNumber,borrowTime,returnTime,deadline,status,operator,userNumber);
                aBoolean= new BookBorrowServiceImpl().updateById(addBookBorrowDto);
            }
        }
        if (aBoolean){
            new AlertUtilCommon().AlterMessageInform(updateBookNumber.getScene().getWindow(), Alert.AlertType.INFORMATION,"修改提醒","修改成功","小仲书提示你:操作成功");
            HomePageController homePageController = new HomePageController();
            homePageController.setMainFrameAnchorPane((AnchorPane) updateBorrowAnchorPane.getParent());
            homePageController.do_queryBorrowingMenuItem_event(null);
        }

    }

    public void resetBorrowTextAction(){
        updateBorrowId.setText("");
        updateBookName.setText("");
        updateBookNumber.getEditor().setText("");
        updateOperator.setText("");
        updateBorrowDate.getEditor().setText("");
        updateDeadline.getEditor().setText("");
        updateUserNumber.getEditor().setText("");
        updateUserName.setText("");
        updateBorrowStatus.setValue("");
        updateReturnDate.getEditor().setText("");
    }

    @FXML
    public void bookNumberReleased(){
        List<String> snackList=bookService.getBookNumberByKeywordWithStatus(updateBookNumber.getEditor().getText());
        ObservableList<String> obList = FXCollections.observableArrayList(snackList);
        updateBookNumber.setItems(obList);
        updateBookNumber.show();
        updateBookName.setText(bookService.getBookNameByNumber(updateBookNumber.getEditor().getText()));
    }

    @FXML
    public void userNumberAction(){
        updateUserName.setText(userService.getUserNameByNumber(updateUserNumber.getEditor().getText()));
        updateUserName.redo();
    }

    @FXML
    public void bookNumberAction(){
        updateUserName.setText(userService.getUserNameByNumber(updateUserNumber.getEditor().getText()));
        updateUserName.redo();
    }

    @FXML
    public void updateBorrowStatusAction(){
        if (updateBorrowStatus.getValue().equals("借用中")){
            updateReturnDate.getEditor().setText("");
            updateReturnDate.setDisable(true);
        }else {
            updateReturnDate.setValue(LocalDate.now());
            updateReturnDate.setDisable(false);
        }
    }

}
